<template>
	<view>
		<view class="richbox">
			<textarea name="txts" id="txts" cols="30" v-model="content" rows="10" placeholder="发个动态，遇见缘分..."></textarea>
		</view>
		<view class="imgbox">
			<view class="itemimg" v-for="(i,k) in files" :key="k">
				<image :src="adminImgPath+i" class="imgs" mode=""></image>
				<view class="cuo">
					X
				</view>
			</view>

			<image src="/static/forum/uploads1.png" v-if="files.length<9" @click="uploadsmenimg" class="imgs" mode=""></image>
		</view>
		<view class="tips">
			*最多上传9张照片
		</view>
		<view class="btnbox" @click="toshenhe">
			发布
		</view>
	</view>
</template>

<script>
	import ikzLoading from '@/pages/Component/Public/Loading/loading.vue';
	var app = getApp( );
	import ikz from '@/pages/Common/tools.js';
	export default {
		components: {
			ikzLoading
		},
		data( ) {
			return {
				adminImgPath: getApp( ).globalData.adminImgPath,
				fileList3: [ {
					url: 'https://uviewui.com/swiper/1.jpg',
				} ],
				files: [ ],
				content: ''
			};
		},
		onShow( ) {

		},
		methods: {

			uploadsmenimg( ) {
				console.log( '门店上传' );
				var that = this;
				uni.chooseImage( {
					count: 1,
					sizeType: [ 'original', 'compressed' ],
					sourceType: [ 'album', 'camera' ],
					success( res ) {
						// tempFilePath可以作为img标签的src属性显示图片
						console.log( res, 'resss' );

						that.upload( res.tempFilePaths[ 0 ] )
					}
				} )
			},
			upload( val ) {
				uni.showLoading( {
					title: '上传中',
					mask: true,
				} )
				uni.uploadFile( {
					url: app.globalData.adminPath + '/common/upload',
					filePath: val,
					name: 'file',
					formData: {
						token: uni.getStorageSync( 'local_user_session' ),
					},
					success: uploadFileRes => {
						if ( uploadFileRes.code == 401 ) {
							console.log( uni.getStorageSync( 'local_user_session' ) );
						} else {
							if(this.files.length<9){
								this.files.push( JSON.parse( uploadFileRes.data ).data.url )
							}
							// images.push( JSON.parse( uploadFileRes.data ).data.url )
							console.log( this.files, 'files' );
						}
					},
					complete: ( ) => {
						uni.hideLoading( )
					},
				} )
			},
			toshenhe( ) {
				var path = '/xyb/posts/posts/add';
				var data = {
					images: this.files,
					content: this.content,
					token: uni.getStorageSync( 'local_user_session' ),
				}; 
				ikz.requestPost( path, data, res => {
					console.log(res,'rrrr');
					if ( res.data.code ) {
						uni.navigateTo( {
							url: '/pages/Home/fabushenhe/fabushenhe'
						} )
					} else {
						uni.showModal( {
							title: '提示',
							content: res.data.msg,
							showCancel: false,
							success: function( res ) {}
						} );
					}
				} )				
			}
		},
	}
</script>

<style lang="scss" scoped>
	.richbox {
		width: 100%;
		height: 326rpx;
		border-bottom: 8rpx solid #F5F5F5;

		#txts {
			width: 100%;
			height: 100%;
			padding: 22rpx 30rpx;
			box-sizing: border-box;
			color: #AFAFAF;
			font-size: 28rpx;
		}
	}

	.tips {
		width: 100%;
		height: 34rpx;
		font-weight: 400;
		font-size: 22rpx;
		color: #AFAFAF;
		text-align: left;
		padding-left: 30rpx;
		box-sizing: border-box;
	}

	.imgbox {
		width: 100%;
		min-height: 200rpx;
		padding: 30rpx 30rpx;
		box-sizing: border-box;
		display: flex;
		flex-wrap: wrap;

		.itemimg {
			// width: 124rpx;
			height: 124rpx;
			margin-right: 18rpx;

			.imgs {
				width: 124rpx;
				height: 124rpx;
				background: #F5F5F5;
				border-radius: 16rpx 16rpx 16rpx 16rpx;
				border: 2rpx solid #F5F5F5;
				position: relative;
			}

			.cuo {
				width: 24rpx;
				height: 24rpx;
				position: relative;
				right: -100rpx;
				top: -136rpx;
			}
		}

		.imgs {
			width: 124rpx;
			height: 124rpx;
			background: #F5F5F5;
			border-radius: 16rpx 16rpx 16rpx 16rpx;
			border: 2rpx solid #F5F5F5;
		}
	}

	.btnbox {
		width: 690rpx;
		height: 88rpx;
		background: #247EFF;
		border-radius: 44rpx 44rpx 44rpx 44rpx;
		position: fixed;
		bottom: 74rpx;
		left: 30rpx;
		font-weight: 400;
		font-size: 30rpx;
		color: #FFFFFF;
		text-align: center;
		line-height: 88rpx;
	}
</style>